package com.course.cdspringboot.controller;


import com.course.cdspringboot.mapper.ReviewMapper;
import com.course.cdspringboot.mapper.achievement.AchievementsMapper;
import com.course.cdspringboot.security.RoleAccessControl;
import com.course.cdspringboot.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/review")
public class ReviewController {

    private static final Logger log = LoggerFactory.getLogger(ReviewController.class);
    @Autowired
    RoleAccessControl roleAccessControl;

    @Autowired
    AchievementsMapper achievementsMapper;

    @Autowired
    ReviewMapper reviewMapper;

    // 教师获取待处理请求
    @GetMapping("")
    public List<Map<String, Object>> getReviewItems(){
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
      return reviewMapper.findReviewItems(uid);
    }

    // 提交审核
    @PatchMapping("/submit")
    public Result updateReview(@RequestBody Map<String, Object> body) {
//        System.out.println(body);
        Long uid = roleAccessControl.checkAccess("ROLE_STUDENT");

        // 查看是否拥有这个成果
        if (achievementsMapper.isUserOwnAchievement(uid, (Integer) body.get("achievementId")) == 0) return Result.update_error().data("msg", "你没有权限修改该成果");

        // 修改成果的审核状态
        int change = achievementsMapper.updateReviewStatus((Integer) body.get("achievementId"), 2, "");
        if (change > 0) return Result.update_ok().data("msg", "提交审核成功");

        return Result.update_error().data("msg", "提交审核失败");

    }

    // 教师拒绝审核
    @PatchMapping("/refuse")
    public Result rejectReview(@RequestBody Map<String, Object> reviewInfo) {
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
        // 查看老师是否有修改审核状态的权限
        if (achievementsMapper.isTeacherOwnAchievement(uid, (Integer) reviewInfo.get("achievementId")) == 0) return Result.update_error().data("msg", "你没有权限修改该成果");

        // 修改成果的审核状态
        int change = achievementsMapper.updateReviewStatus((Integer) reviewInfo.get("achievementId"), 4, (String) reviewInfo.get("reason"));
        if (change > 0) return Result.update_ok().data("msg", "拒绝审核成功");

        return Result.update_error().data("msg", "拒绝审核失败");
    }

    // 教师通过审核
    @PatchMapping("/pass")
    public Result passReview(@RequestBody Map<String, Object> reviewInfo) {
//        System.out.println(reviewInfo);
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
        // 查看老师是否有修改审核状态的权限
        if (achievementsMapper.isTeacherOwnAchievement(uid, (Integer) reviewInfo.get("achievementId")) == 0) return Result.update_error().data("msg", "你没有权限修改该成果");

        // 修改成果的审核状态
        int change = achievementsMapper.updateReviewStatus((Integer) reviewInfo.get("achievementId"), 3, "");
        if (change > 0) return Result.update_ok().data("msg", "通过审核成功");

        return Result.update_error().data("msg", "通过审核失败");
    }

}
